package com.handinhand.mobile;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.handinhand.mobile.conn.HandInHandCallback;
import com.handinhand.mobile.model.User;
import com.handinhand.mobile.service.UserService;

public class SignUpActivity extends BaseActivity {
	
	private Button signupButton;
	private Button loginNavButton;	
	private EditText signupName;
	private EditText signupSurname;
	private EditText signupUsername;
	private EditText signupEmail;
	private EditText signupPassword;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// Check if a user already logged in.
		HandInHandApplication application = (HandInHandApplication) getApplication();
		if (application.getLoggedUser() != null) {
			Intent intent = new Intent(SignUpActivity.this, HomeActivity.class);
			startActivity(intent);
			finish();
		}

		setContentView(R.layout.activity_sign_up);

		setupUI();
	}

	private void setupUI() {
		signupButton = (Button) findViewById(R.id.btnSignUp);
		signupButton.setOnClickListener(onSignUpButtonClick);

		loginNavButton = (Button) findViewById(R.id.btnLoginNav);
		loginNavButton.setOnClickListener(onloginNavButtonClick);
		
		signupName = (EditText) findViewById(R.id.etName);
		signupSurname = (EditText) findViewById(R.id.etSurname);
		signupUsername = (EditText) findViewById(R.id.etUsername);
		signupEmail = (EditText) findViewById(R.id.etEmail);
		signupPassword = (EditText) findViewById(R.id.etPassword);
	}
	
	private OnClickListener onloginNavButtonClick = new OnClickListener() {
		@Override
		public void onClick(View v) {
			Intent intent = new Intent(SignUpActivity.this, MainActivity.class);
			startActivity(intent);
			finish();
		}
	};
	
	private OnClickListener onSignUpButtonClick = new OnClickListener() {
		@Override
		public void onClick(View v) {
			String name = signupName.getText().toString();
			String surname = signupSurname.getText().toString();
			String username = signupUsername.getText().toString();
			String email = signupEmail.getText().toString();
			String password = signupPassword.getText().toString();			
			

			if (username.trim().equals("") || password.trim().equals("") || email.trim().equals("")) {
				Toast.makeText(SignUpActivity.this, "Username, password and e-mail address cannot be empty!", Toast.LENGTH_SHORT).show();
				return;
			}

			UserService userService = new UserService(getApp());
			userService.signup(name, surname, username, email, password, new HandInHandCallback<User>() {
				public void onSuccess(User response) {
					setLoggedUser(response);
					System.out.println("-------------------------------------------------------------------------" + "SUCCESS");
					Intent intent = new Intent(SignUpActivity.this, HomeActivity.class);
					startActivity(intent);
					finish();
				}

				public void onFail(String errorMessage) {
					System.out.println("-------------------------------------------------------------------------" + "FAIL");
					Toast.makeText(SignUpActivity.this, errorMessage, Toast.LENGTH_SHORT).show();
				}
			});
		}
	};
}
